'''
# Coding:utf-8
# Project: work-platform
# Author: rtf
# Time: 2020-10-27 14:32:58
# FileName: recursion_tree.py
# Software: PyCharm
'''


def get_list(parent_id, datas, parent_column_name="parent_id", next_parent_column_name="id"):
    result = list()
    for x in datas:
        if str(x.get(parent_column_name)) == str(parent_id):
            next_pid = x.get(next_parent_column_name)
            x['children'] = get_list(next_pid, datas, parent_column_name, next_parent_column_name)
            result.append(x)
    return result


def get_list_child(parent_id, data, parent_column_name="parent_id", next_parent_column_name="id"):
    result = list()
    for x in data:
        if str(x.get(parent_column_name)) == str(parent_id):
            next_pid = x.get(next_parent_column_name)
            children = get_list_child(next_pid, data, parent_column_name, next_parent_column_name)
            result.append(x)
            result.extend(children)
    return result


def get_list_parent(children_id, data, parent_column_name="parent_id", next_parent_column_name="id"):
    result = list()
    for x in data:
        if x.get(next_parent_column_name) == children_id:
            next_pid = x.get(parent_column_name)
            parent = get_list_parent(next_pid, data, parent_column_name, next_parent_column_name)
            result.append(x)
            result.extend(parent)
    return result